﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    public class CHierarchy
    {
        #region Fields
        
        [ContentSerializer]
        protected int m_nodesCount;

        [ContentSerializer]
        protected CAnimNode[] m_nodes;

        #endregion

        #region Property

        [ContentSerializerIgnore]
        public CAnimNode[] Nodes
        {
            get { return this.m_nodes; }
        }

        public int NodesCount
        {
            get { return this.m_nodesCount; }
        }

        public CAnimNode this[int i]
        {
            get
            {
                return this.m_nodes[i];
            }
        }

        public int this[string name]
        {
            get
            {
                return m_nodes.IndexOfByValue(name);
            }
        }

        #endregion

        #region CTOR

        public CHierarchy()
        {

        }

        public CHierarchy(CAnimNode[] _bones)
        {
            this.m_nodes = _bones;
            this.m_nodesCount = _bones.Length;
        }

        #endregion

        #region Method

        public CAnimNode FindNodeByName(string name)
        {
            return (from el in m_nodes where el.Name == name select el).SingleOrDefault();
        }

        public List<int> FindSkeletonBoneIndex(CHierarchy _bone)
        {
            if (_bone.NodesCount > m_nodes.Length)
                return null;

            List<int> _b = new List<int>();

            for (int j = 0; j < _bone.NodesCount; ++j)
            {
                for (int i = 0; i < m_nodesCount; ++i)
                {
                    if (m_nodes[i].Name == _bone[j].Name)
                    {
                        _b.Add(i);
                        break;
                    }
                }
            }
            return _b;
        }

        public int GetIndexByName(string name)
        {
            return this.m_nodes.IndexOf((from el in m_nodes where el.Name == name select el).SingleOrDefault());
        }

        #endregion

    }
}
